package ch10.d2;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
// 字节流
public class FileInputStreamTest {

    @Test
    public void testRead0() throws IOException {
        FileInputStream fis = new FileInputStream("javakc107.txt");
        int n = 0;
        n = fis.read();
        System.out.println(n);

        fis.close();
    }

    @Test
    public void testRead() throws IOException {
        FileInputStream fis = new FileInputStream("javakc107.txt");
        int n = 0;

        while ((n = fis.read()) != -1) {
            System.out.println(n);
        }

        fis.close();
    }

    @Test
    public void testRead2() throws IOException {
        //创建文件字节输入流对象
        FileInputStream fis = new FileInputStream("javakc107.txt");
        //创建字节数组
        byte[] b = new byte[40];
        // 读取字节数
        int n = 0;
        //读取字节到字节数组中
        n = fis.read(b);
        // 字节数组转换成字符串
        String s = new String(b);

        System.out.println(Arrays.toString(b));
        System.out.println(s);

    }

    @Test
    public void testRead3() throws IOException {
        //创建文件字节输入流对象
        FileInputStream fis = new FileInputStream("javakc107.txt");


        //读取所有字节
        byte[] b = fis.readAllBytes();
        // 字节数组转换成字符串
        String s = new String(b);

        System.out.println(Arrays.toString(b));
        System.out.println(s);

    }
    @Test
    public void testRead4() throws IOException {
        //创建文件字节输入流对象
        FileInputStream fis = new FileInputStream("javakc107.txt");


        //读取所有字节
        byte[] b = new byte[3];
        int n=0;
        while ((n=fis.read(b))!=-1){

            String s=new String(b,0,n);
            System.out.print(s);
        }

        fis.close();


    }
}
